var ChartQuery = function (containerId, tree, showEigen) {
    var self = this;
    self.tree = tree;
    self.showEigen = showEigen;
    self.containerId = containerId;
    self.points = new Array();
    self.addTreeNode = function (treeNode) {
        if (treeNode.nodeType == 4) {
            //动态测点
            ajaxPost(contextRoot + "dynamicPoint/getDetailDynamicPoint/" + treeNode.id, {}, function (data) {
                if (self.showEigen == false)
                    self.addNoEigenNodeHtml(treeNode, data);
                else
                    self.addNodeHtml(treeNode, data);
            });
        }
        else if (treeNode.nodeType == 5) {
            //静态测点
            ajaxPost(contextRoot + "staticPoint/getDetailStaticPoint/" + treeNode.id, {}, function (data) {
                if (self.showEigen == false)
                    self.addNoEigenNodeHtml(treeNode, data);
                else
                    self.addNodeHtml(treeNode, data);
            });
        }
    };
    self.addNodeHtml = function (treeNode, data) {
        var deviceCode = self.getDeviceNode(treeNode).code;
        var point = {
            pointId: data.entity.id,
            pointName: data.entity.name,
            deviceCode: deviceCode,
            pointCode: data.entity.code,
            color: '#f00'
        };
        self.points.push(point);

        var itemHtmlFormat =
            '<tr>' +
            '<td>{0}</td>' +
            '<td>{1}</td>' +
            '<td>{2}</td>' +
            '<td>{3}</td>' +
            '<td>{4}</td>' +
            '</tr>';

        var uuid = +new Date().getTime();
        var eigensHtml = '<select id="sl_eigen_item_' + uuid + '" style="margin-top:10px;">';
        $.each(data.eigenCheckedValues, function (i, d) {
            eigensHtml += String.format('<option value="{0}">{1}</option>', d.code, d.name);
        });
        eigensHtml += '</select>';

        var colorPickerHtml = "<input id='cp_eigen_item_" + uuid + "' type='text' class='colorpicker' value='red'/>";

        var actionHtml = '<button id="btn_remove_' + uuid + '" class="layui-btn layui-btn-sm"><i class="layui-icon"></i></button>';
        var itemHtml = String.format(itemHtmlFormat, data.entity.name,
            data.entity.code, eigensHtml, colorPickerHtml, actionHtml);
        $("#" + self.containerId).append(itemHtml);

        $("#cp_eigen_item_" + uuid).spectrum({
            change: function (color) {
                point.color = color.toHexString();
            }
        });

        $("#btn_remove_" + uuid).click(function () {
            self.removeTreeNode(point, this);
        });

        $("#sl_eigen_item_" + uuid).change(function () {
            point.eigenCode = $(this).children('option:selected').val();
        });
        $("#sl_eigen_item_" + uuid).trigger("change");
    };
    self.addNoEigenNodeHtml = function (treeNode, data) {
        var deviceCode = self.getDeviceNode(treeNode).code;
        var point = {
            pointId: data.entity.id,
            pointName: data.entity.name,
            deviceCode: deviceCode,
            pointCode: data.entity.code,
            color: '#f00'
        };
        self.points.push(point);

        var itemHtmlFormat =
            '<tr>' +
            '<td>{0}</td>' +
            '<td>{1}</td>' +
            '<td>{2}</td>' +
            '<td>{3}</td>' +
            '</tr>';

        var uuid = +new Date().getTime();
        var colorPickerHtml = "<input id='cp_eigen_item_" + uuid + "' type='text' class='colorpicker' value='red'/>";

        var actionHtml = '<button id="btn_remove_' + uuid + '" class="layui-btn layui-btn-sm"><i class="layui-icon"></i></button>';
        var itemHtml = String.format(itemHtmlFormat, data.entity.name,
            data.entity.code, colorPickerHtml, actionHtml);
        $("#" + self.containerId).append(itemHtml);

        $("#cp_eigen_item_" + uuid).spectrum({
            change: function (color) {
                point.color = color.toHexString();
            }
        });

        $("#btn_remove_" + uuid).click(function () {
            self.removeTreeNode(point, this);
        });
    }
    ;
    self.removeTreeNode = function (point, btn) {
        var tr = $(btn).parents("tr");
        tr.remove();
        self.points.splice($.inArray(point, self.points), 1);
    };
    self.getDeviceNode = function (treeNode) {
        return self.tree.getDeviceNode(treeNode);
    };
}